package com.icim.twyd.importer.service.impl;

import java.io.File;
import java.util.List;




import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.icim.entity.Ret_cus_box;
import com.icim.entity.Ret_cus_prd;
import com.icim.twyd.importer.service.IRetCusBoxService;
import com.icim.twyd.importer.service.IRetCusPrdService;
import com.icim.twyd.importer.service.IRetXmlImportService;
import com.icim.twyd.importer.xmlentity.TwydCellXmlEntity;
import com.icim.twyd.importer.xmlentity.TwydMessageXmlEntity;
import com.icim.util.TimeUtil;
import com.icim.util.XmlConvertUtil;

@Transactional
@Service("retXmlImportService")
public class RetXmlImportServiceImpl implements IRetXmlImportService {
  
  @Autowired
  private IRetCusBoxService retCusBoxService;
  @Autowired
  private IRetCusPrdService retCusPrdService;

  @Override
  public void importXMLProc(File xmlFile) {
      // 解析XML，将xml转换为对象实体
    TwydMessageXmlEntity xmlEntity = (TwydMessageXmlEntity) XmlConvertUtil.file2Object1(xmlFile,
        TwydMessageXmlEntity.class);
  
    Ret_cus_box retCusBox = new Ret_cus_box();
    retCusBox.TwydXml2Entity(xmlEntity.getBOX());
    // 写入DB
    retCusBox.setEvt_timestamp(TimeUtil.getEvtTime());
    retCusBoxService.save(retCusBox);
    
    List<TwydCellXmlEntity> cellList = xmlEntity.getBOX().getCELL();
    for (TwydCellXmlEntity cell : cellList) {
      Ret_cus_prd retCusPrd = new Ret_cus_prd();
      retCusPrd.TwydXml2Entity(cell, retCusBox.getBoxid());
      // 写入DB
      retCusPrd.setEvt_timestamp(TimeUtil.getEvtTime());
      retCusPrdService.save(retCusPrd);
    }
  }
}
